package com.zlyx.easy.dev.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.context.properties.ConfigurationProperties;

import com.zlyx.easy.core.utils.HostUtils;
import com.zlyx.easy.core.utils.MethodUtils;
import com.zlyx.easy.core.utils.ProxyUtils;
import com.zlyx.easy.dev.remote.RemoteUtils;

@ConfigurationProperties("easy.dev")
public abstract class AbstractDevAspect {

	private String host = HostUtils.getHost();

	private boolean open = false;

	/**
	 * 提示：重写此方法来增加新的切面内容(不支持自定义泛型对象)
	 */
	@Pointcut("@annotation(com.zlyx.easy.dev.annotations.EasyDev)")
	public void devAspect() {
	}

	@Around("devAspect()")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		Signature signature = pjp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method targetMethod = methodSignature.getMethod();
		Class<?> realClass = ProxyUtils.getTarget(pjp.getTarget()).getClass();
		Method realMethod = MethodUtils.getRealMethod(realClass, targetMethod);
		return open || !isProcced() ? RemoteUtils.doProcess(host, realMethod, pjp.getArgs()) : pjp.proceed();
	}

	protected abstract boolean isProcced();

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}

}
